﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QLBH.ModelInterface;
using QLBH.DataModel;
using QLBH.Model.Controller;
using QLBH.ModelFactory;
using QLBH.Category;

namespace QLBH.Category
{
    public partial class FrmSelectCustomer : Form
    {
        private IKhachHangModel selectedKH;

        public IKhachHangModel SelectedKH
        {
            get { return selectedKH; }
            set { selectedKH = value; }
        }
        private List<IKhachHangModel> khachhangModels;
        public FrmSelectCustomer()
        {
            InitializeComponent();

            khachhangModels = new List<IKhachHangModel>();
        }

        private void groupBox1_Enter(object sender, EventArgs e)
        {

        }

        private void FrmSelectCustomer_Load(object sender, EventArgs e)
        {
            LoadKhachHangs();
        }
        private void LoadKhachHangs()
        {
            List<KHACHHANG> khachhangs = KhachHangController.GetInstance().FindBy(kh => kh.KHCODE != "KL").ToList();
            foreach (KHACHHANG kh in khachhangs)
            {
                IKhachHangModel model = SaleProductModelFactory.GetKhachHangModelInstance();
                model.InitFromEntity(kh);

                khachhangModels.Add(model);
            }

            dataGridViewEx1.DataSource = khachhangModels;
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            String customerName = tbxTenKH.Text;
            string sdt = tbxSDT.Text;

            if (customerName != "" && sdt != "")
            {
                var result = khachhangModels.Where(kh => kh.TENKH.Contains(customerName) && kh.DIDONG.Equals(sdt)).ToList();

                dataGridViewEx1.DataSource = result;

            }
            else if (customerName != "")
            {
                var result = khachhangModels.Where(kh => kh.TENKH.Contains(customerName)).ToList();

                dataGridViewEx1.DataSource = result;

            }
            else if (sdt != "")
            {
                var result = khachhangModels.Where(kh => kh.DIDONG.Equals(sdt)).ToList();

                dataGridViewEx1.DataSource = result;
            }
            else
            {
                dataGridViewEx1.DataSource = khachhangModels;
            }

        }

        private void btnSelect_Click(object sender, EventArgs e)
        {
            SelectedKhachHang();
        }

        private void btnAddNew_Click(object sender, EventArgs e)
        {            
            AddKhachHangDlg addKhachHangDlg = new AddKhachHangDlg();
            if (addKhachHangDlg.ShowDialog()==DialogResult.OK)
            {
                LoadKhachHangs();
            }
        }

        private void FrmSelectCustomer_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                btnSearch_Click(btnSearch, null);
            }
            else if (e.KeyCode==Keys.F1)
            {
                if (btnSelect.Enabled)
                {
                    btnSelect_Click(btnSelect, null);
                }                
            }
            else if (e.KeyCode==Keys.F2)
            {
                btnAddNew_Click(btnAddNew, null); 
            }
            else if (e.KeyCode==Keys.Escape)
            {
                this.DialogResult = DialogResult.Cancel;
                this.Close();
            }
        }

        private void dataGridViewEx1_CellMouseDoubleClick(object sender, DataGridViewCellMouseEventArgs e)
        {
            SelectedKhachHang();           
        }
        private void SelectedKhachHang()
        {
            if (dataGridViewEx1.SelectedRows.Count > 0)
            {
                selectedKH = dataGridViewEx1.SelectedRows[0].DataBoundItem as IKhachHangModel;
                this.DialogResult = DialogResult.OK;
                this.Close();
            } 
        }

        private void dataGridViewEx1_SelectionChanged(object sender, EventArgs e)
        {
            if (dataGridViewEx1.SelectedRows.Count>0)
            {
                btnSelect.Enabled = true;
            }
            else
            {
                btnSelect.Enabled = false;
            }
        }
    }
}
